package br.com.agiledev.webscrum.action;

import java.util.Arrays;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import br.com.agiledev.webscrum.eao.ProductEAO;
import br.com.agiledev.webscrum.entity.Product;
import br.com.agiledev.webscrum.entity.Release;

@SuppressWarnings("serial")
@Name("releaseSearchMB")
@Scope(ScopeType.PAGE)
public class ReleaseSearchAction extends QueryAction<Release> {

    private static final String EJBQL = "select re from Release re";
    private static final String[] RESTRICTIONS = { "lower(re.nome) like lower(concat(#{releaseSearchMB.release.nome}, '%'))",
	"re.product = #{releaseSearchMB.release.product}"};
    private static final String ORDER_COLUMN = "re.product.nome, re.nome";

    private Release release;

    private List<Product> products;
    
    @In
    ProductEAO productEAO;
    
    public ReleaseSearchAction() {
	setEjbql(EJBQL);
	setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
	setOrder(ORDER_COLUMN);
    }

    // Get's / Set's
    public Release getRelease() {
	if(release == null) {
	    release = new Release();
	}
	return release;
    }

    public void setRelease(Release release) {
	this.release = release;
    }

    public List<Product> getProducts() {
	if(products == null) {
	    products = productEAO.findAll();
	}
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

}